package com.carrey.cms_1811.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.carrey.cms_1811.entity.Article;
import com.carrey.cms_1811.entity.User;
import com.carrey.cms_1811.service.ArticleService;
import com.carrey.cms_1811.service.UserService;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("admin")
public class AdminController {

	
	@Autowired
	private ArticleService articleService;
	
	@Autowired
	private UserService userService;
	
	// 跳转到管理员中心首页
    @RequestMapping(value = { "/", "", "index" })
    public String index() {
          return "admin/index";
    }
    // 所有文章展示
    @RequestMapping("articles")
    public String articles(Model m, Article article,  @RequestParam(defaultValue = "1") Integer pageNum,
               @RequestParam(defaultValue = "5") Integer  pageSize) {
          // 如果status的值是2
          if (article.getStatus() != null && article.getStatus()  == 2) {
               article.setStatus(null);
          }
          // 查询数据
          PageInfo<Article> info = articleService.getArticles(pageNum,  pageSize, article);
          m.addAttribute("info", info);
          m.addAttribute("article", article);
          return "admin/articles";
    }
    // 修改文章状态
    @RequestMapping("update")
    @ResponseBody
    public boolean update(Article article) {
          // 设置修改时间
          article.setUpdated(new Date());
          return articleService.update(article);
    }
    // 用户管理
    @RequestMapping("users")
    public String users(Model m, User user,  @RequestParam(defaultValue = "1") Integer pageNum,
               @RequestParam(defaultValue = "5") Integer  pageSize) {
          // 如果locked的值是2
          if (user.getLocked() != null && user.getLocked() == 2)  {
               user.setLocked(null);
          }
          // 查询数据
          PageInfo<User> info = userService.getUsers(pageNum,  pageSize, user);
          m.addAttribute("info", info);
          m.addAttribute("user", user);
          return "admin/users";
    }
    // 修改用户的锁定状态
    @RequestMapping("updateLocked")
    @ResponseBody
    public boolean updateLocked(User user) {
          // 设置修改时间
          user.setUpdated(new Date());
          return userService.updateLocked(user);
    }

}
